/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-17 14:00:30  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.edu.repository;

import java.util.List;

import com.joysuch.wwyt.edu.entity.EduTrainningRecordUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

/**
 * EduTrainningRecordUserDao
 *
 */
public interface EduTrainningRecordUserDao
        extends JpaRepository<EduTrainningRecordUser, Long>, QuerydslPredicateExecutor<EduTrainningRecordUser> {
    public Page<EduTrainningRecordUser> findAll(Specification<EduTrainningRecordUser> spec, Pageable pageable);

    public EduTrainningRecordUser save(EduTrainningRecordUser bean);

    public void delete(EduTrainningRecordUser bean);

    public int deleteByTrainningRecordId(Long id);

    public List<EduTrainningRecordUser> findByTrainningRecordId(Long id);

    @Modifying
    @Query("update EduTrainningRecordUser set certUserId=null where certUserId=?1")
    public void setCertUserIdToNull(Long id);

    @Modifying
    @Query(value = "update wwyt_edu_trainning_record_user set delete_flag = '1' where id in ?1", nativeQuery = true)
    void deleteByIds(List<Long> ids);

    @Query(value = "select IFNULL(SUM(wetr.LESSONS)*3600,0) as lessons  from wwyt_edu_trainning_record_user wetru LEFT JOIN wwyt_edu_trainning_record wetr ON wetru.TRAINNING_RECORD_ID=wetr.ID where wetru.USER_ID=?1 and wetru.DELETE_FLAG='0' AND wetr.DELETE_FLAG='0'  " , nativeQuery = true)
    public Long getEduTrainningRecordUserByUserId(Long userId);

    @Query(value = "select * from wwyt_edu_trainning_record_user where TRAINNING_RECORD_ID = ?1 and USER_ID=?2 and DELETE_FLAG='0' limit 1" , nativeQuery = true)
    EduTrainningRecordUser getEduTrainingRecordUser(Long trainingRecordId, Long userId);

}
